<template>
  <div v-if="config" class="query-link">
    <div class="table-title">
      <!-- <b>{{ $srv.getI18nByPath('piece.oQueryLinkSettingq.jointinvestigationsettings') }}</b> -->
      <Icon
        class="custom-btn-add"
        type="ios-add-circle"
        @click="addQueryLink"
      />
      <!-- <Button size="small" @click="addQueryLink">添加</Button> -->
    </div>

    <div class="rules-cell" v-for="(item, idx) of config" :key="idx">
      <div class="rules-cell-title">
        <o-i18n-selector
          v-model="item.title"
          @on-dochange="handleConfigChange"
          :placeholder="
            $srv.getI18nByPath(
              'piece.oQueryLinkSettingq.pleaseenterthejointname'
            )
          "
        ></o-i18n-selector>
        <o-m2o-selector
          :placeholder="
            $srv.getI18nByPath(
              'piece.oQueryLinkSettingq.pleaseenterthefunctionname'
            )
          "
          v-model="item.funName"
          tTable="_sys_fun"
          tField="title"
          vField="title"
          @on-dochange="handleConfigChange"
        ></o-m2o-selector>
        <div class="rules-cell-btns">
          <Button size="small" @click="addQueryLinkChildren(idx)">
            {{
              $srv.getI18nByPath('piece.oQueryLinkSettingq.increasecondition')
            }}
          </Button>
          <Icon type="md-close" @click="removeQueryLink(idx)" />
        </div>
      </div>
      <template v-if="item.children">
        <div
          class="rules-cons query-link-children"
          v-for="(child, iidx) of item.children"
          :key="'child' + iidx"
        >
          <div class="rules-cons-inputs">
            <div class="rules-cons-fields">
              <i-input
                :placeholder="
                  $srv.getI18nByPath('piece.oQueryLinkSettingq.reportfieldname')
                "
                v-model="child.field"
                @on-blur="handleConfigChange"
              ></i-input>
              <i-input
                :placeholder="
                  $srv.getI18nByPath('piece.oQueryLinkSettingq.targetfieldname')
                "
                v-model="child.toField"
                @on-blur="handleConfigChange"
              ></i-input>
            </div>
          </div>
          <div class="rules-res-btns">
            <Icon type="md-close" @click="removeQueryLinkChildren(idx, iidx)" />
          </div>
        </div>
      </template>
    </div>
  </div>
</template>

<script lang="ts">
import { Component, Prop, Watch, Model, Vue } from 'vue-property-decorator';
import { Tsrv } from '@/services';

@Component
export default class oQueryLinkSetting extends Vue {
  @Model('modelchange') config: any;

  mounted() {}

  private handleConfigChange() {
    this.$emit('modelchange', this.config);
    this.$emit('change');
  }

  private addQueryLinkChildren(index: number) {
    this.config[index].children.push({
      field: '',
      toField: ''
    });
    this.$emit('modelchange', this.config);
    this.$emit('change');
  }

  private addQueryLink() {
    this.config.push({
      title: '',
      funName: '',
      children: []
    });
    this.$emit('modelchange', this.config);
    this.$emit('change');
  }

  /**
   * 移除联查
   */
  private removeQueryLink(index: number) {
    this.config.splice(index, 1);
    this.$emit('modelchange', this.config);
    this.$emit('change');
  }

  /**
   * 移除联查子节点
   */
  private removeQueryLinkChildren(index: number, subIndex: number) {
    this.config[index].children.splice(subIndex, 1);
    this.$emit('modelchange', this.config);
    this.$emit('change');
  }
}
</script>

<style scoped lang="scss">
.query-link {
  .table-title {
    // margin-top: 10px;
    height: 30px;
    line-height: 30px;
    .ivu-icon {
      float: left;
      margin-top: 5px;
      font-size: 18px;
    }
  }
  .custom-btn-add {
    cursor: pointer;
  }
  .rules-cell {
    padding-top: 5px;
    .ivu-icon {
      cursor: pointer;
      font-size: 18px;
    }
    .rules-cell-title {
      width: 100%;
      height: 30px;
      line-height: 30px;
      display: flex;
      justify-content: space-between;
      span {
        float: left;
      }
      ::v-deep .ivu-input-wrapper {
        width: 100px;
        .ivu-input {
          background-color: transparent;
          border-top: 0;
          border-left: 0;
          border-right: 0;
        }
      }
      .rules-cell-btns {
        float: right;
        display: flex;
        align-items: center;
        button {
          margin-right: 10px;
        }
      }
    }
    .rules-cons {
      margin: 10px 0;
      display: flex;
      .rules-cons-title {
        line-height: 30px;
        flex-shrink: 0;
      }
      .rules-cons-inputs {
        margin: 0 10px;
        flex: 1;
        .rules-cons-fields {
          margin-bottom: 10px;
        }
        .rules-cons-fields,
        .rules-cons-values {
          display: flex;
          > div:first-child {
            margin-right: 10px;
          }
        }
      }
      .rules-cons-btns {
        flex-shrink: 0;
        line-height: 30px;
      }
    }
    .query-link-children {
      .rules-cons-inputs {
        margin-left: 0;
        .rules-cons-fields {
          margin-bottom: 5px;
          > div:first-child {
            margin-right: 10px;
          }
        }
      }
    }
    .header-group-children {
      .rules-cons-inputs {
        margin-left: 0;
        .rules-cons-fields {
          margin-bottom: 5px;
          > div:first-child {
            margin-right: 0;
          }
        }
      }
    }
    .rules-res {
      margin: 10px 0;
      display: flex;
      .rules-res-title {
        line-height: 30px;
        flex-shrink: 0;
      }
      .rules-res-inputs {
        margin: 0 10px;
        flex: 1;
        .rules-res-type {
          margin-bottom: 10px;
        }
        .rules-res-details {
          display: flex;
          flex-direction: column;
          > div {
            display: flex;
            justify-content: space-between;
            margin: 5px 0 10px;
          }
        }
      }
      .rules-res-btns {
        flex-shrink: 0;
        line-height: 30px;
      }
    }
  }
}
</style>
